var MenuList = {
	data: {},
	init: function() {
		var submitFunc = function ($dialog){
			var err = 0;
			$('input[type!=hidden][type!=submit][disabled=false][name!=url]', $dialog).each(function (){
				if (err==0 && this.value =='') {
					err++;
					alert('所有选项都不能为空!');
					this.focus();
				}
			});
			
			return err == 0;
		}
		this.data = new $.dataManage({
			key: 'menuId',
			dialog:{
			    width: 500
			},
			add:{
				title:'添加菜单',
				click: function(form){
					form.find("input[name='parentId']").val(form.data('parentId'));
					form.find("select[name='moduleName'] option[value=" + form.data('moduleName') + "]")
						.attr('selected', true)
						.siblings().removeAttr('selected');
					return true;
				},
				success: function (data, form, selectors) {
		            $newItem = $(selectors.template).clone().show().removeAttr('id');
		            for (var i in data.params) {
		                var val = data.params[i];
		                if ($('[name='+i+']', form).attr('tagName') == 'SELECT') {
		                    val = $('[name='+i+'] option[value='+data.params[i]+']', form).html();
		                }
		                $newItem.html($newItem.html().replace(new RegExp('#'+i+'#', 'ig'), val));
		            }
		            var deep = $(form).data('deep');
		            $newItem.html($newItem.html().replace('#deep#', deep + 1));
		            $newItem.find("td[name='name']").attr('style', 'text-indent: ' + (deep + 1) * 24 + 'px;');
		            if(deep >= 2) {
		            	$newItem.find('button:first')
		            		.attr('onclick', '')
		            		.addClass('ig-button-disabled');
		            }
		            var parentId = $("input[name='parentId']", form).val();
		            if(parentId == 0) {
		            	$(selectors.list).append($newItem);
		            } else {
			            $current = $("#node-" + $("input[name='parentId']", form).val());
			            $current.after($newItem);
		            }
		        },
				submit: submitFunc
			},
			edit:{
				title:'编辑菜单"#name#"',
				click: function(form){
					form.find("input[name='parentId']").val(form.data('parentId'));
					form.find("select[name='moduleName'] option[value=" + form.data('moduleName') + "]")
						.attr('selected', true)
						.siblings().removeAttr('selected');
					$("#menu-module-name").hide();
					return true;
				},
				submit: submitFunc
			}
		});
	},
	add: function (deep, parentId, moduleName){
		this.data.$dialog.data('deep', deep);
		this.data.$dialog.data('parentId', parentId);
		this.data.$dialog.data('moduleName', moduleName);
		if(parentId > 0) {
			this.data.$dialog.find("tr[id='menu-module-name']").hide();
		} else {
			this.data.$dialog.find("tr[id='menu-module-name']").show();
		}
		this.data.add();
	},
	edit: function (parentId, moduleName, obj) {
		this.data.$dialog.data('parentId', parentId);
		this.data.$dialog.data('moduleName', moduleName);
		if(parentId > 0) {
			this.data.$dialog.find("tr[id='menu-module-name']").hide();
		} else {
			this.data.$dialog.find("tr[id='menu-module-name']").show();
		}
		$obj = (obj instanceof $) ? obj : $(obj).parent().parent();
		this.data.edit($obj.find("td[name='menuId']").html(), obj);
	}
};
$(function (){
	MenuList.init();
});